﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.Department
{
    /// <summary>
    /// NewStaff 的摘要说明。
    /// </summary>
    public partial class NewStaffPage : Page
    {
        public static string DeptID;
        private static int EditStatus;
        private static int sex = 1;
        private int StaffID;
        private int ReturnPage;

        protected override void OnInit(EventArgs e)
        {
            cmdSubmit.Click += cmdSubmit_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //绑定部门
                BindDepartment();

                if (Request.QueryString["DeptID"] != null)
                {
                    DeptID = Request.QueryString["DeptID"];
                    SelectDepartment(Int32.Parse(DeptID));
                }
                else
                {
                    DeptID = "0";
                }

                txtBirthday.Text = DateTime.Now.ToShortDateString();

                if (Request.QueryString["StaffID"] != null)
                {
                    StaffID = Int32.Parse(Request.QueryString["StaffID"]);
                    GetStaffInfo(StaffID);
                    EditStatus = 1;
                }
                else
                {
                    EditStatus = 0;
                    sex = 1;
                }
            }

            if (Request.QueryString["ReturnPage"] != null)
            {
                ReturnPage = 1;
            }

            //if(DeptID!="0")
            //mydepartment.Visible = false;
            base.OnLoad(e);
        }

        private void BindDepartment()
        {
            var db = new Database();
            using (SqlDataReader dr_department = db.RunDataReader("dbo.sproc_GetAllDepartment"))
            {
                cboDepartment.DataSource = dr_department;
                cboDepartment.DataTextField = "Department_Name";
                cboDepartment.DataValueField = "Department_ID";
                cboDepartment.DataBind();
            }
        }

        private void GetStaffInfo(int id)
        {
            txtPassword.TextMode = TextBoxMode.SingleLine;
            txtRePassword.TextMode = TextBoxMode.SingleLine;

            var staffInfo = Staff.GetStaffInfo(id);
            if (staffInfo != null)
            {
                txtStaffName.Text = staffInfo.Name;
                txtStaffName.ReadOnly = true;
                txtRealName.Text = staffInfo.RealName;
                if (staffInfo.Sex != UDS.Entity.Sex.Female)
                {
                    rb_male.Checked = true;
                    sex = 1;
                }
                else
                {
                    rb_female.Checked = true;
                    sex = 0;
                }

                txtBirthday.SelectedDate = staffInfo.Birthday;
                txtPassword.Text = staffInfo.Password;
                txtRePassword.Text = staffInfo.Password;
                txtEmail.Text = staffInfo.Email;
                txtPhone.Text = staffInfo.Phone;
                txtMobile.Text = staffInfo.Mobile;

                // TODO:
                // Need another API to get department information?
                //
                throw new InvalidOperationException("Department info not found?");
                // SelectDepartment(Int32.Parse(dr["Department_ID"].ToString()));
            }
        }

        private void SelectDepartment(int DepartmentID)
        {
            for (int i = 0; i < cboDepartment.Items.Count; i++)
            {
                if (Int32.Parse(cboDepartment.Items[i].Value) == DepartmentID)
                {
                    cboDepartment.SelectedIndex = i;
                }
            }
        }

        private void cmdSubmit_Click(object sender, EventArgs e)
        {
            if (rb_male.Checked)
            {
                sex = 1;
            }
            else
            {
                sex = 0;
            }

            if (EditStatus == 0)
            {
                var db = new Database();
                if (mydepartment.Visible)
                {
                    DeptID = cboDepartment.Items[cboDepartment.SelectedIndex].Value;
                }

                var prams = new[]
                {
                    Database.MakeInParam("@StaffName", SqlDbType.NVarChar, 50, txtStaffName.Text),
                    Database.MakeInParam("@Password", SqlDbType.NVarChar, 255, txtPassword.Text),
                    Database.MakeInParam("@RealName", SqlDbType.NVarChar, 50, txtRealName.Text),
                    Database.MakeInParam("@Sex", SqlDbType.Int, 4, sex),
                    Database.MakeInParam("@Status", SqlDbType.Int, 4, 0),
                    Database.MakeInParam("@Email", SqlDbType.NVarChar, 300, txtEmail.Text),
                    Database.MakeInParam("@RegistedDate", SqlDbType.DateTime, 8, DateTime.Now),
                    Database.MakeInParam("@DeptID", SqlDbType.Int, 4, Int32.Parse(DeptID)),
                    Database.MakeInParam("@Phone", SqlDbType.NVarChar, 50, txtPhone.Text),
                    Database.MakeInParam("@Mobile", SqlDbType.NVarChar, 50, txtMobile.Text),
                    Database.MakeInParam("@Birthday", SqlDbType.DateTime, 8, Utility.GetValidSQLDate(txtBirthday.SelectedDate))
                };
                using (SqlDataReader dr = db.RunDataReader("dbo.sproc_AddStaff", prams))
                {
                    if (dr.Read())
                    {
                        if (ReturnPage == 0)
                        {
                            Response.Redirect("ListView.aspx?Dep_ID=" + DeptID);
                        }
                        else
                        {
                            Response.Redirect("../Staff/ManageStaff.aspx");
                        }
                    }
                    else
                    {
                        message.Text = "该用户已经存在！";
                    }
                }
            }
        }
    }
}